package vsy.buzzwordbingo.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import vsy.buzzwordbingo.client.GameFieldWindow;

public class BuzzwordWindow {

	private JPanel jContentPane = null;  
	private ArrayList<String> buzzwords;  //  @jve:decl-index=0:
	private int amountOfBuzzwords = -1;
	private int amountcopy;
	private int actualBuzzword = 1;
	private JFrame jFrame = null;  
	private JPanel jContentPane1 = null;
	private JTextField jTextField = null;
	private JLabel jLabel = null;
	private JButton jButton = null;
	private JLabel jLabel1 = null;
	private JPanel jPanel1 = null;
	private ServerListWindow CserverList;
	private String title = "";  
	private String beschreibung = ""; 
	
	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}
	
	public BuzzwordWindow(String title, String beschreibung, ServerListWindow sl) { // gibt einfach nur die ServerListen Klasse weiter, zudem Spielinfos
		this.title = title;
		this.beschreibung = beschreibung;
		this.CserverList = sl;
	}
	
	/**
	 * This method initializes jFrame	
	 * 	
	 * @return javax.swing.JFrame	
	 */
	public JFrame getJFrame(int amountOfBuzzwords) {
		if (jFrame == null) {
			this.amountOfBuzzwords = (amountOfBuzzwords*amountOfBuzzwords);
			if (!(this.amountOfBuzzwords%2==0)){
				this.amountOfBuzzwords-=1;
			}
			this.amountcopy = amountOfBuzzwords;
			jFrame = new JFrame();
			jFrame.setSize(new Dimension(800, 600));
			jFrame.setTitle("Buzzword Bingo");
			jFrame.setContentPane(getJContentPane1());
			jFrame.setVisible(true);
			jFrame.setLocationRelativeTo(null);
			jFrame.setResizable(false);
			jLabel1.setText("(" + (actualBuzzword) + "/" + this.amountOfBuzzwords + ")");
			buzzwords = new ArrayList<String>();
			jFrame.setContentPane(getJPanel1());
			jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
				public void windowClosing(java.awt.event.WindowEvent e) {
					System.out.println("Logout");
					GameClient.logout();
					System.exit(0);
				}
			});
			}
		return jFrame;
	}
	
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new BackGroundPane("src/main/images/buzzwordbingo_serverlist_bg.jpg");
			jPanel1.setLayout(null);
			jPanel1.add(jLabel, null);
			jPanel1.add(getJTextField(), null);
			jPanel1.add(jLabel, null);
			jPanel1.add(getJButton(), null);
			jPanel1.add(jLabel1, null);
		}
		return jPanel1;
	}
	
	/**
	 * This method initializes jContentPane1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane1() {
		if (jContentPane1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(527, 226, 166, 28));
			jLabel1.setText("(" + actualBuzzword + "/" + amountOfBuzzwords + ")");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(236, 224, 126, 33));
			jLabel.setText("Buzzword eintragen:");
			jContentPane1 = new JPanel();
			jContentPane1.setLayout(null);
			jContentPane1.add(getJTextField(), null);
			jContentPane1.add(jLabel, null);
			jContentPane1.add(getJButton(), null);
			jContentPane1.add(jLabel1, null);
		}
		return jContentPane1;
	}
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(361, 225, 167, 31));
		}
		return jTextField;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(361, 256, 167, 26));
			jButton.setText("naechstes Buzzword");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String word = jTextField.getText();
					if (jButton.getText().compareTo("Spiel starten") != 0) { 
						if (actualBuzzword < amountOfBuzzwords) {
							buzzwords.add(word);
							actualBuzzword++;
							jLabel1.setText("(" + actualBuzzword + "/" + amountOfBuzzwords + ")");
						} else {
							buzzwords.add(word);
							jLabel1.setText("");
							jButton.setText("Spiel starten");
						} 
					} else {
						// Spiel starten
						Runnable runnable = new GameFieldWindow(buzzwords, amountcopy, CserverList, beschreibung, title);
						ServerListWindow.setThread(new Thread(runnable));					
						jFrame.setVisible(false);
						
						GameFieldWindow game = (GameFieldWindow)runnable;
						game.setVisible(true);
						ServerListWindow.game = game;
						jFrame.setVisible(false);
						game.setVisible(true);
					}
				}
			});
		}
		return jButton;
	}

}
